import {Injectable} from "injection-js";
import {Mut, VueService} from "vue3-oop";
import RoleApi from "../RoleApi"
import AllcPrmsSercice from "./panel/allcprms.service";


@Injectable()
export default class RoleDetailService extends VueService {

  constructor(
    public allcPrmsService:AllcPrmsSercice
  ){
    super();
  }

  @Mut() state = {
    editable: false,
    srcData: {
      id:"", code:"", name:""
    },
    editData: {
      id:"", code:"", name:""
    }
  }

  /**
   * 切换编辑模式
   */
  toggleEditable = ()=>{
    this.state.editData = {...this.state.srcData};
    this.state.editable = !this.state.editable;
  }

  /**
   * 保存编辑数据
   */
  saveEditData = ()=>{
    RoleApi.update({record:this.state.editData}).then(({data})=>{
      if(data.success){
        window.$message.success("更新成功");
        this.loadUserData();
      }
      this.state.editable = false;
    })
  }

  /**
   * 加载用户信息
   */
  loadUserData=()=>{
    const currentRoute = window.$router.currentRoute.value;
    RoleApi.get(currentRoute.params.id).then(({data})=>{
      if(data.success && data.record){
        const {id, name, code} = data.record;
        this.state.srcData = {id, name, code};
        this.state.editData = {...this.state.srcData};

        // this.allcRoleService.init(id,personname);
        this.allcPrmsService.init(id,name);
        // this.validPrmsService.init(id,personname);
      }
    });
  }

}